package ar.com.fiuba.domain;

import ar.com.fiuba.domain.exceptions.DomainException;

/**
 * Concrete strategy to calculate common coeficient. TODO explain
 * 
 */
public class CommonCoeficientCalculationWithPositionStrategy implements CoeficientCalculationStrategy {

	double positionScore = 0.2;
	
	public Double calculate(Word word, String anotherWord, Integer ngrammLength) throws DomainException {
		int equalNgramms = 0;
		double extraScore = 0;
		Word shortestWord;
		Word largestWord;

		// Set which is the longer word, to iterate over it.
		if (word.getValue().length() > anotherWord.length()) {
			largestWord = word;
			shortestWord = new Word(anotherWord, ngrammLength);
		} else {
			shortestWord = word;
			largestWord = new Word(anotherWord, ngrammLength);
		}

		int position = 0;	
		// Obtain number of equal ngramms.
		for (Ngramm ngram : shortestWord.getNgramms()) {
			if (largestWord.getNgramms().contains(ngram)) {
				equalNgramms++;
				if(ngram.getValue().equalsIgnoreCase(largestWord.getNgramms().get(position).getValue()))	
					extraScore += positionScore;	
			}
			position++;
		}
		return (double) (equalNgramms + extraScore) / (double) largestWord.getNgramms().size();
	}

	public void setPositionScore(double positionScore) {
		this.positionScore = positionScore;
	}

}

